package so.sao.core;


import net.sf.json.JSONObject;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共返回数据类
 * @author gxy.
 */
public class ReturnMessage {
    private int code;
    private JSONObject data;

    public ReturnMessage() {
    }

    /**
     * 初始化,只包含code
     *
     * @param code
     */
    public ReturnMessage(int code) {
        this.code = code;
    }

    /**
     * 初始化一条错误信息, 包含代码和返回信息文本
     * tyk
     *
     * @param code 错误代码
     * @param msg  错误信息
     */
    public ReturnMessage(int code, String msg) {
        this.code = code;
        initDataObject();
        data.put("msg", msg);
    }

    /**
     * 向返回消息中添加参数
     *
     * @param name   参数名
     * @param object 对象
     */
    public void add(String name, Object object) {
        initDataObject();
        this.data.put(name, object);
    }

    public void add(Object object) throws Exception {
        initDataObject();
        this.data.putAll(convertBean(object));
    }

    /**
     * 添加返回消息
     *
     * @param msg 消息内容
     */
    public void add(String msg) {
        initDataObject();
        this.data.put("msg", msg);
    }

    public int getCode() {
        return code;
    }

    public JSONObject getData() {
        return this.data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }

    /**
     * 初始化内部data对象
     */
    private void initDataObject(){
        if(this.data == null) {
            data = new JSONObject();
        }
    }

    /**
     * 将一个 JavaBean对象转化为Map
     *
     * @param bean 要转化的JavaBean 对象
     * @return 转化出来的  Map 对象
     * @throws IntrospectionException    如果分析类属性失败
     * @throws IllegalAccessException    如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    private Map convertBean(Object bean)
            throws IntrospectionException, IllegalAccessException, InvocationTargetException {

        if(bean instanceof  Map){
            return (Map)bean;
        }

        Class type = bean.getClass();
        Map returnMap = new HashMap();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(bean, new Object[0]);
                if (result != null) {
                    returnMap.put(propertyName, result);
                } else {
                    returnMap.put(propertyName, "");
                }
            }
        }
        return returnMap;
    }
}